/**
 * Testclass
 * @author Lukas Koell
 * @author Johannes Riedmann
 * @author David Medenjak
 */

public class Test {

    public static void main(String[] args){
        
        // first company - terminates regularly
        Lager kuehlhaus = new Lager(1);
        Lager tisch = new Lager(2);
        Lager steinbruch = new Lager(1);
        Dorfplatz dorfplatz = new Dorfplatz(1000);
        
        System.out.println("Testing Company that terminates regularly\n");
        System.out.println("Wildschweinjaegers should produce 14 units.");
        System.out.println("Koch should produce 70 units.");
        System.out.println("Baeumfaellers should produce 70 units.");
        System.out.println("Logistikers should produce 70 units.\n\n");
        
        Unternehmen u1 = new Unternehmen(7);
        Thread t1 = new Thread(u1);
        t1.start();
        
        sleep(2000);
        
        System.out.println("\n\n--------------------------------------------------------------\n\n");
        
        System.out.println("Testing Company that terminates regularly");
        System.out.println("... with changed storage-sizes, working time of all workers always differ (we use random numbers)\n");
        
        // second company - terminates regularly
        kuehlhaus = new Lager(200);
        tisch = new Lager(100);
        steinbruch = new Lager(100);
        dorfplatz = new Dorfplatz(1000);
        
        System.out.println("Testing Company that terminates regularly\n");
        System.out.println("Wildschweinjaegers should produce 20 units.");
        System.out.println("Koch should produce 100 units.");
        System.out.println("Baeumfaellers should produce 100 units.");
        System.out.println("Logistikers should produce 100 units.\n\n");
        
        Unternehmen u2 = new Unternehmen(10, kuehlhaus, tisch, steinbruch, dorfplatz);
        Thread t2 = new Thread(u2);
        t2.start();
                
        sleep(2000);
        
        System.out.println("\n\n--------------------------------------------------------------\n\n");
        
        System.out.println("Testing Company that terminates by interrupt");
        System.out.println("... with changed storage-sizes\n");
        
        // third company - termination interrupted
        kuehlhaus = new Lager(200);
        tisch = new Lager(100);
        steinbruch = new Lager(100);
        dorfplatz = new Dorfplatz(1000);
        
        System.out.println("(When terminated regularly):");
        System.out.println("Wildschweinjaegers should produce 40 units.");
        System.out.println("Koch should produce 200 units.");
        System.out.println("Baeumfaellers should produce 200 units.");
        System.out.println("Logistikers should produce 200 units.\n\n");
        
        Unternehmen u3 = new Unternehmen(20, kuehlhaus, tisch, steinbruch, dorfplatz);
        Thread t3 = new Thread(u3);
                
        t3.start();
        testing interrupt = new testing(u3);
        new Thread(interrupt).start();
    }
    
    /**
     * thread sleeps to delay further actions
     * @param milliseconds time the current thread is sleeping
     */
    private static void sleep(int milliseconds) {
        try {
            Thread.sleep(milliseconds);
        } catch (InterruptedException e1) {
            e1.printStackTrace();
        }
    }
    
    /**
     * tests company closure during runtime
     */
    public static class testing implements Runnable{
        private Unternehmen u;
        
        public testing(Unternehmen u){
            this.u = u;
        }
        public void run(){
            sleep(200);
            u.kill();
            sleep(2000);
            System.out.println("\n\n(a few units could have been dropped because workers got fired during work)");
        }
    }
}
